gl root = "~/Documents/Harvard/vbm"

* Prepare an analysis dataset with each row being a state-year-office
use "$root/data/analysis/analysis.dta", clear
keep state county county_id year state_year dem_share* treat year2
keep if inlist(state, "CA", "WA", "UT")
reshape long dem_share, i(state county year) j(office) s

* Diff-in-diff with state-year fixed effects, Dem vote share
// Table 2, Col 4
reghdfe dem_share treat, ///
	a(county_id state_year) vce(clust county_id)
local b4 = _b[treat]
local se4 = _se[treat]
local n4 = e(N)
distinct county_id if e(sample)
local nc4 = r(ndistinct)
distinct state_year if e(sample)
local ne4 = r(ndistinct)

* Diff-in-diff with state-year fixed effects and linear trends, Dem vote share
// Table 2, Col 5	
reghdfe dem_share treat, ///
	a(county_id state_year county_id##c.year) ///
	vce(clust county_id)
local b5 = _b[treat]
local se5 = _se[treat]
local n5 = e(N)
distinct county_id if e(sample)
local nc5 = r(ndistinct)
distinct state_year if e(sample)
local ne5  = r(ndistinct)

* Diff-in-diff with state-year fixed effects and quadratic trends, Dem vote share
// Table 2, Col 6
reghdfe dem_share treat, ///
	a(state_year county_id county_id##c.year county_id##c.year2) ///
	vce(clust county_id)
local b6 = _b[treat]
local se6 = _se[treat]
local n6 = e(N)
distinct county_id if e(sample)
local nc6 = r(ndistinct)
distinct state_year if e(sample)
local ne6  = r(ndistinct)


* Build the table
quietly {
	cap log close
	set linesize 255
	log using "$root/exhibits/voteshare_table_original_states_stata.tex", text replace
	
	noi di "\begin{table}[t]"
	noi di "\centering"
	noi di "\caption{\textbf{Vote-by-Mail Expansion Favors Democratic Candidates (CA, UT, WA).}\label{tab:voteshare_orig_stata}}"
	noi di "\begin{tabular}{llccc}"
	noi di "\toprule \toprule"
	noi di " & \multicolumn{3}{c}{Dem Vote Share [0-1]} \\"
	noi di " & (1) & (2) & (3)  \\"
	noi di "\midrule"
	noi di "VBM & " %4.3f `b4' " & " %4.3f `b5' " & " %4.3f `b6'  " \\"
	noi di " & (" %4.3f `se4' ") & (" %4.3f `se5' ") & (" %4.3f `se6' ")  \\[2mm]"
	noi di "\# Counties & " %8.0fc `nc4' " & " %8.0fc `nc5' " & " %8.0fc `nc6' " \\"
	noi di "\# Elections & " %8.0fc `ne4' " & " %8.0fc `ne5' " & " %8.0fc `ne6' " \\"
	noi di "\# Obs & " %8.0fc `n4' " & " %8.0fc `n5' " & " %8.0fc `n6' " \\"
	noi di "\midrule"
	noi di "County FE & Yes & Yes & Yes \\"
	noi di "State by Year FE & Yes & Yes & Yes \\"
	noi di "County Trends & No & Linear & Quad\\"
	noi di "\bottomrule \bottomrule"
	noi di "\multicolumn{4}{p{.5\textwidth}}{\footnotesize{Robust standard errors clustered by county in parentheses.  "
	noi di "Analysis includes only California, Utah, and Washington. }}"
	noi di "\end{tabular}"
	noi di "\end{table}"
	
	log off
}
